#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
	int x=0;char ch=getchar();
	while(!isdigit(ch)) ch=getchar();
	while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return x;
}
inline void write(int x){
	if(x==0){putchar('0');return;}
	char ch[20];int cnt=-1;
	while(x){
		ch[++cnt]=(x%10)^48;
		x/=10;
	}
	for(int i=cnt;~i;--i) putchar(ch[i]);
}
struct TopTree{
	int s[800005],tag[800005];
	string name;
	TopTree(string ss){name=ss;memset(s,0,sizeof(s));memset(tag,0,sizeof(tag));}
	void clear(){memset(s,0xdf,sizeof(s));memset(tag,0,sizeof(tag));}
	void pushdown(int o){
		int t=tag[o];
		if(t){
			tag[o]=0;
			s[o<<1]+=t;
			s[o<<1|1]+=t;
			tag[o<<1]+=t;
			tag[o<<1|1]+=t;
		}
	}
	void change(int o,int l,int r,int x,int y){
		// if(o==1) cerr<<"change "<<x<<" "<<y<<endl;
		if(l==r){
			s[o]=y;
			return;
		}
		pushdown(o);
		int mid=l+r>>1;
		if(x<=mid) change(o<<1,l,mid,x,y);
		else change(o<<1|1,mid+1,r,x,y);
		s[o]=max(s[o<<1],s[o<<1|1]);
	}
	void add(int o,int l,int r,int L,int R,int x){
		// if(o==1) cerr<<"add "<<L<<" "<<R<<" "<<x<<endl;
		if(l>=L&&r<=R){
			s[o]+=x;
			tag[o]+=x;
			return;
		}
		pushdown(o);
		int mid=l+r>>1;
		if(L<=mid) add(o<<1,l,mid,L,R,x);
		if(R>mid) add(o<<1|1,mid+1,r,L,R,x);
		s[o]=max(s[o<<1],s[o<<1|1]);
	}
	int ask(int o,int l,int r,int L,int R){
		if(l>=L&&r<=R) return s[o];
		pushdown(o);
		int mid=l+r>>1;
		if(R<=mid) return ask(o<<1,l,mid,L,R);
		else if(L>mid) return ask(o<<1|1,mid+1,r,L,R);
		else return max(ask(o<<1,l,mid,L,R),ask(o<<1|1,mid+1,r,L,R));
	}
};
int T,n,a[200005],b[200005],mp[1000005],m;
TopTree dp("dp");
signed main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	T=read();
	while(T--){
		n=read();
		for(int i=1;i<=n;++i) b[i]=a[i]=read();
		sort(b+1,b+n+1);
		m=0;
		for(int i=1;i<=n;++i)
			if(b[i]!=b[i-1]) mp[b[i]]=++m;
		dp.clear();
		dp.change(1,1,m,mp[a[1]],0);
		for(int i=2;i<=n;++i){
			// cerr<<"i="<<i<<endl;
			if(a[i]==a[i-1]){
				dp.add(1,1,m,1,m,a[i]);
			}else{
				int x=dp.ask(1,1,m,mp[a[i]],mp[a[i]]);
				x=max(x+a[i],dp.s[1]);
				int cur=dp.ask(1,1,m,mp[a[i-1]],mp[a[i-1]]);
				if(x>cur) dp.add(1,1,m,mp[a[i-1]],mp[a[i-1]],x-cur);
			}
		}
		write(dp.s[1]);putchar('\n');
	}
	return 0;
}
/*
15
1 12 11 11 7 11 15 6 4 6 3 15 7 5 2
*/